<?php

$maxRow = sizeof($_POST["snEnt"]);

/**
 * check out if entry exists then
 * it is a modified type
 * then clean up
 */
$CLFetchers->subDtlSerNumber($Db,
                             $_GET["tmpJid"],
                             $SourceDb->getValue("srcDbTmpSubDtlSN"),
                             $nData,
                             $lstData);

if($nData > 0){

  $CHandlers->dbEntDelete($Db,
                          $_GET["tmpJid"],
                          $SourceDb->getValue("srcDbTmpSubDtlSN"));

  $CHandlers->dbEntDelete($Db,
                          $_GET["tmpJid"],
                          $SourceDb->getValue("srcDbSubDtlComposSN"));
}else{

  /*Do nothing*/
}

/**
 * If parent model id is not exist
 * then it is not composition
 */
if(!$_GET["parentModelId"]){

  for($iRow = 0; $iRow < $maxRow ; $iRow++){

    if($_POST["snEnt"][$iRow]){

      /**
       * check if this warehouse has this
       * Serial Number
       */
      $CLFetchers->fetchWhsHasSn($Db,
                                 $_GET["srcWhs"],
                                 $_POST["snEnt"][$iRow],
                                 $SourceDb->getValue("srcDbSerialNumber"),
                                 $retSN);

      if($retSN == true){

        $values = " '".$_GET["tmpJid"]."',  ".
                  " '".$_GET["modelId"]."', ".
                  " '".($iRow+1)."',        ".
                  " '".$_POST["snEnt"][$iRow]."', ";

        $CLHandlers->insToSubDtlSN($Db,
                                   $values,
                                   $SourceDb->getValue("srcDbTmpSubDtlSN"));
      }else{

        $Misc->usrError("Error: This SN ".$_POST["snEnt"][$iRow].
                        " not exist in ". $_GET["srcWhs"]);

        $Db->setStatus(false);

        break;
      }
    }else{

      /* Do nothing */
    }
  }
}else{

  /**
   * it is composition
   */
  for($iRow = 0; $iRow < $maxRow ; $iRow++){

    if($_POST["snEnt"][$iRow]){

      /**
       * check if this warehouse has this
       * Serial Number
       */
      $CLFetchers->fetchWhsHasSn($Db,
                                 $_GET["srcWhs"],
                                 $_POST["snEnt"][$iRow],
                                 $SourceDb->getValue("srcDbSerialNumber"),
                                 $retSN);

      if($retSN == true){

        $values = " '".$_GET["tmpJid"]."',  ".
                  " '".$_GET["modelId"]."', ".
                  " '".($iRow+1)."',        ".
                  " '".$_POST["snEnt"][$iRow]."', ";

        $CLHandlers->insToSubDtlComposSN($Db,
                                         $values,
                                         $SourceDb->getValue("srcDbTmpSubDtlComposSN"));
      }else{

        $Misc->usrError("Error: This SN ".$_POST["snEnt"][$iRow].
                        " not exist in ". $_GET["srcWhs"]);

        $Db->setStatus(false);

        break;
      }
    }else{

      /* Do nothing */
    }
  }
}

//$Db->commit();

if($Db->getStatus()){

  #echo  $GJs->jsScript(NULL,"this.window.close();");
}else{

  $Misc->usrMsgJs("Upload Process Interupted");
}

?>